package com.flowerimprove;

import java.util.ArrayList;
import java.util.Scanner;

public class FlowerIndex implements FlowerTool{
    //创建键盘录入数据
    public static final Scanner input = new Scanner(System.in);

    //创建集合
    public static ArrayList<FlowerOrder> list = new ArrayList<>();

    static {
        list.add(new FlowerOrder(1001, "金色海岸 - 香槟玫瑰", 10, 318.0, "2016-12-12", "王二昵", ""));
        list.add(new FlowerOrder(1002, "初心永不变 - 白玫瑰", 99, 538.0, "2016-12-20", "张欣雨", ""));
        list.add(new FlowerOrder(1003, "梦里花开 - 紫色桔梗", 40, 148.0, "2016-12-24", "王二昵", "眼波如水，映出窗外繁星点点"));
        list.add(new FlowerOrder(1004, "心花怒放 - 粉香百合", 120, 188.0, "2016-12-26", "王二昵", "内层银灰色厚棉纸，外层深蓝色条纹纸，银灰色缎带"));
        list.add(new FlowerOrder(1005, "致青春 - 无声的爱 ",1000, 219.0, "2016-12-18", "张欣雨", "致青春 - 无声的爱；我爱你，胜过爱爱情，更胜过爱自己！"));
    }

    public static void index() {
        FlowerIndex f = new FlowerIndex();
        outer:
        //主页面
        while (true) {
            System.out.println("=======================欢迎光临\"七彩鲜花\"销售管理系统=====================");
            System.out.println("1.查询销售订单");
            System.out.println("2.添加销售订单");
            System.out.println("3.修改销售订单");
            System.out.println("4.删除销售订单");
            System.out.println("5.系统退出");
            System.out.println("======================================================================");

            //键盘输入一个整数：choice，选择功能
            int choice = input.nextInt();
            //判断功能选择
            switch (choice) {
                case 1:
                    //1.查询销售订单 findAll()
                    f.findAll();
                    break;
                case 2:
                    //2.添加销售订单 add()
                    f.add();
                    break;
                case 3:
                    //3.修改销售订单 update()
                    f.update();
                    break;
                case 4:
                    //4.删除销售订单 delete()
                    f.delete();
                    break;
                default: //5.系统退出
                    f.exit();
                    break outer;
            }
        }
    }
    //查询销售订单
    @Override
    public void findAll() {
        //打印表头
        System.out.println("编号\t\t鲜花名称\t\t\t\t销售数量\t\t价格\t\t销售日期\t\t销售员\t备注");
        for (FlowerOrder flowerOrder : list) {
            System.out.println("" + flowerOrder.getId() + "\t" + flowerOrder.getFlowerName() + "\t" + flowerOrder.getCount() + "\t\t\t" + flowerOrder.getPrice() + "\t" + flowerOrder.getDate() + "\t" + flowerOrder.getEmployeeName() + "\t" + flowerOrder.getDescription());
        }
        System.out.println();
        System.out.println();
    }

    //判断是否存在
    public boolean select(int id) {
        for (FlowerOrder flowerOrder : list) {
            if (flowerOrder.getId() == id) {
                return true;
            }
        }
        return false;
    }
    //添加销售订单
    public void add() {
        System.out.println("请输入您想要添加的订单编号");
        int id = input.nextInt();
        if (select(id)){
            System.out.println("您输入的订单编号已存在");
            return;
        }
        System.out.println("请输入您想要添加的订单名称");
        String flowerName = input.next();
        System.out.println("请输入您想要添加的订单数量");
        int count = input.nextInt();
        System.out.println("请输入您想要添加的订单价格");
        double price = input.nextDouble();
        System.out.println("请输入您想要添加的订单日期");
        String date = input.next();
        System.out.println("请输入您想要添加的订单销售员");
        String employeeName = input.next();
        System.out.println("请输入您想要添加的订单备注");
        String description = input.next();
        list.add(new FlowerOrder(id, flowerName, count, price, date, employeeName, description));
        System.out.println("添加成功");
        findAll();
    }

    //删除销售订单
    public void delete() {
        System.out.println("请输入您想要删除的订单编号");
        int id = input.nextInt();
        if (select(id)) {
            int index = 0;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId() == id) {
                    index = i;
                }
            }
            System.out.println("请输入您想要删除的订单信息如下：");
            System.out.println("编号\t\t鲜花名称\t\t\t\t销售数量\t\t价格\t\t销售日期\t\t销售员\t备注");
            System.out.println("" + list.get(index).getId() + "\t" +
                    list.get(index).getFlowerName() + "\t" +
                    list.get(index).getCount() + "\t\t\t" +
                    list.get(index).getPrice() + "\t" +
                    list.get(index).getDate() + "\t" +
                    list.get(index).getEmployeeName() + "\t" +
                    list.get(index).getDescription());
            System.out.println("是否删除该订单？y/n");
            String choice = input.next();
            if (choice.equals("y")) {
                list.remove(index);
                System.out.println("删除成功");
                findAll();
                return;
            }else {
                System.out.println("已取消删除");
                findAll();
                return;
            }
        }
        System.out.println("您输入的订单编号不存在");
    }

    //修改销售订单
    public void update() {

        System.out.println("请输入您想要修改的订单信息id：");
        int id = input.nextInt();
        if (select(id)) {
            //获取索引和删除一样的方法
            int index = 0;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId() == id) {
                    index = i;
                }
            }
            System.out.println("您想要修改的订单信息为：");
            System.out.println("编号\t\t鲜花名称\t\t\t\t销售数量\t\t价格\t\t销售日期\t\t销售员\t备注");
            System.out.println("" + list.get(index).getId() + "\t" +
                    list.get(index).getFlowerName() + "\t" +
                    list.get(index).getCount() + "\t\t\t" +
                    list.get(index).getPrice() + "\t" +
                    list.get(index).getDate() + "\t" +
                    list.get(index).getEmployeeName() + "\t" +
                    list.get(index).getDescription());
                    id = input.nextInt();
                    System.out.println("请输入您想要修改的订单名称");
                    String flowerName = input.next();
                    System.out.println("请输入您想要修改的订单数量");
                    int count = input.nextInt();
                    System.out.println("请输入您想要修改的订单价格");
                    double price = input.nextDouble();
                    System.out.println("请输入您想要修改的订单日期");
                    String date = input.next();
                    System.out.println("请输入您想要修改的订单销售员");
                    String employeeName = input.next();
                    System.out.println("请输入您想要修改的订单备注");
                    String description = input.next();
                    list.set(index, new FlowerOrder(id, flowerName, count, price, date, employeeName, description));
            System.out.println("请确认修改");
            System.out.println("y/n");
            String choice = input.next();
            if (choice.equals("y")) {
                list.set(index, new FlowerOrder(id, flowerName, count, price, date, employeeName, description));
                System.out.println("修改成功");
                findAll();
                return;
            }
            System.out.println("已取消修改");
            findAll();
            return;
        }
        System.out.println("您输入的订单编号不存在");
    }

    //退出系统
    public void exit() {
        System.err.println("系统退出成功，谢谢您的使用");
    }
}
